﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMISLibrary;
using BCNPCObject;
using BCNPCObject.DB;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Forms;

namespace bus_TienichNPC
{
    public class cls_NPC_TRTH_CHISO
    {
        #region   Atributes

        private cls_NPC_TRTH_CHISO_Controller obj_NPC_TRTH_CHISO_Controller;
        private NPC_TRTH_CHISO obj_NPC_TRTH_CHISO_Info;

        #endregion


        #region   Constructor

        
        public cls_NPC_TRTH_CHISO()
        {
            obj_NPC_TRTH_CHISO_Info = new NPC_TRTH_CHISO();
            obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
        }
        public cls_NPC_TRTH_CHISO(DataSet entity)
        {
            obj_NPC_TRTH_CHISO_Info = new NPC_TRTH_CHISO();
            obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
        }       

        #endregion

        #region Method

        public DataSet select_NPC_TRTH_CHISO_All(string strMadviqly)
        {
            try
            {
                if (obj_NPC_TRTH_CHISO_Controller == null)
                {
                    obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
                }

                var q = from p in obj_NPC_TRTH_CHISO_Controller.select_NPC_TRTH_CHISO_All(strMadviqly)
                        select new
                        {
                            p.ID_CHISO,
                            p.MA_DVIQLY,
                            p.ID_TRTH,
                            p.CHISO_CU,
                            p.CHISO_MOI,
                            p.SAN_LUONG,
                            p.NGAY_DKY,
                            p.GIO_DKY,
                            p.PHUT_DKY,
                            p.NGAY_CKY,
                            p.GIO_CKY,
                            p.PHUT_CKY,
                            p.HSN,
                            p.BCS,
                            p.SO_CTO,
                            p.MA_CTO,
                            p.KY,
                            p.LOAI_CHISO,
                            p.NGUOI_TAO,
                            p.NGAY_TAO
                        };

                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "NPC_TRTH_CHISO";
                    dt.AcceptChanges();
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    return ds;
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

        public DataSet select_NPC_TRTH_CHISO_by_id_truythu(long intID)
        {
            try
            {
                if (obj_NPC_TRTH_CHISO_Controller == null)
                {
                    obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
                }               

                var q = from p in obj_NPC_TRTH_CHISO_Controller.select_NPC_TRTH_CHISO_by_id_truythu(intID)
                        select new 
                        {
                            p.ID_CHISO,
                            p.MA_DVIQLY,
                            p.ID_TRTH,
                            p.CHISO_CU,
                            p.CHISO_MOI,
                            p.SAN_LUONG,
                            p.NGAY_DKY,
                            p.GIO_DKY,
                            p.PHUT_DKY,
                            p.NGAY_CKY,
                            p.GIO_CKY,
                            p.PHUT_CKY,
                            p.HSN,
                            p.BCS,
                            p.SO_CTO,
                            p.MA_CTO,
                            p.KY,
                            p.LOAI_CHISO,
                            p.NGUOI_TAO,
                            p.NGAY_TAO
                        };

                var m = obj_NPC_TRTH_CHISO_Controller.kythangnam(intID);

                

                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    dt.TableName = "NPC_TRTH_CHISO";
                    dt.AcceptChanges();
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);

                    if (m != null && m.Count() > 0)
                    {
                        var kythangnam = from p in m
                                         where p.ID_TRTH == intID
                                         group p by new { p.KY } into grp
                                         select new
                                         {
                                             grp.Key.KY
                                         };
                        DataTable dt2 = Utility.LINQToDataTable(kythangnam);
                        dt2.TableName = "KYTHANGNAM";
                        dt2.AcceptChanges();                        
                        ds.Tables.Add(dt2);
                    }

                    return ds;
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

        public bool insert_NPC_TRTH_CHISO(DataSet Entity, ref string strErr)
        {
            try
            {
                this.mapEntityToInfo(Entity);
                if (obj_NPC_TRTH_CHISO_Controller == null)
                {
                    obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
                }
                return obj_NPC_TRTH_CHISO_Controller.insert_NPC_TRTH_CHISO(ref strErr);
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                return false;
            }
        }

        public bool update_NPC_TRTH_CHISO(DataSet Entity, ref string strErr)
        {
            try
            {
                this.mapEntityToInfo(Entity);
                if (obj_NPC_TRTH_CHISO_Controller == null)
                {
                    obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
                }
                return this.obj_NPC_TRTH_CHISO_Controller.update_NPC_TRTH_CHISO(ref strErr);
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                return false;
            }

        }

        public bool delete_NPC_TRTH_CHISO(DataSet Entity, ref string strErr)
        {
            try
            {
                this.mapEntityToInfo(Entity);
                if (obj_NPC_TRTH_CHISO_Controller == null)
                {
                    obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
                }
                return this.obj_NPC_TRTH_CHISO_Controller.delete_NPC_TRTH_CHISO(ref strErr);
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                return false;
            }
        }

        public long GetNextSEQ_NPC_TRTHU_CHISO(ref string strErr)
        {
            try
            {
                if (obj_NPC_TRTH_CHISO_Controller == null)
                {
                    obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
                }

                long id = obj_NPC_TRTH_CHISO_Controller.GetNextSEQ_NPC_TRTHU_CHISO(ref strErr);
                return id;
            }
            catch
            {
                return -1;
            }
        }

        public void mapEntityToInfo(DataSet Entity)
        {
            if (this.obj_NPC_TRTH_CHISO_Controller == null)
            {
                this.obj_NPC_TRTH_CHISO_Controller = new cls_NPC_TRTH_CHISO_Controller();
            }
            if (Entity.Tables[0].Rows[0]["ID_CHISO"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.ID_CHISO = Convert.ToInt64(Entity.Tables[0].Rows[0]["ID_CHISO"]);
            }

            this.obj_NPC_TRTH_CHISO_Controller.pInfor.MA_DVIQLY = Entity.Tables[0].Rows[0]["MA_DVIQLY"].ToString();
            this.obj_NPC_TRTH_CHISO_Controller.pInfor.ID_TRTH = Convert.ToInt64(Entity.Tables[0].Rows[0]["ID_TRTH"]);

            if (Entity.Tables[0].Rows[0]["CHISO_CU"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.CHISO_CU = Convert.ToDecimal(Entity.Tables[0].Rows[0]["CHISO_CU"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.CHISO_CU = null;
            }

            if (Entity.Tables[0].Rows[0]["CHISO_MOI"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.CHISO_MOI = Convert.ToDecimal(Entity.Tables[0].Rows[0]["CHISO_MOI"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.CHISO_MOI = null;
            }

            if (Entity.Tables[0].Rows[0]["SAN_LUONG"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.SAN_LUONG = Convert.ToDecimal(Entity.Tables[0].Rows[0]["SAN_LUONG"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.SAN_LUONG = null;
            }

            if (Entity.Tables[0].Rows[0]["NGAY_DKY"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.NGAY_DKY = Convert.ToDateTime(Entity.Tables[0].Rows[0]["NGAY_DKY"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.NGAY_DKY = null;
            }

            if (Entity.Tables[0].Rows[0]["GIO_DKY"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.GIO_DKY = Convert.ToInt16(Entity.Tables[0].Rows[0]["GIO_DKY"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.GIO_DKY = 0;
            }

            if (Entity.Tables[0].Rows[0]["PHUT_DKY"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.PHUT_DKY = Convert.ToInt16(Entity.Tables[0].Rows[0]["PHUT_DKY"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.PHUT_DKY = 0;
            }

            if (Entity.Tables[0].Rows[0]["NGAY_CKY"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.NGAY_CKY = Convert.ToDateTime(Entity.Tables[0].Rows[0]["NGAY_CKY"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.NGAY_CKY = null;
            }

            if (Entity.Tables[0].Rows[0]["GIO_CKY"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.GIO_CKY = Convert.ToInt16(Entity.Tables[0].Rows[0]["GIO_CKY"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.GIO_CKY = 0;
            }

            if (Entity.Tables[0].Rows[0]["PHUT_CKY"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.PHUT_CKY = Convert.ToInt16(Entity.Tables[0].Rows[0]["PHUT_CKY"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.PHUT_CKY = 0;
            }

            if (Entity.Tables[0].Rows[0]["HSN"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.HSN = Convert.ToInt16(Entity.Tables[0].Rows[0]["HSN"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.HSN = 0;
            }

            this.obj_NPC_TRTH_CHISO_Controller.pInfor.BCS = Entity.Tables[0].Rows[0]["BCS"].ToString();
            this.obj_NPC_TRTH_CHISO_Controller.pInfor.SO_CTO = Entity.Tables[0].Rows[0]["SO_CTO"].ToString();
            this.obj_NPC_TRTH_CHISO_Controller.pInfor.MA_CTO = Entity.Tables[0].Rows[0]["MA_CTO"].ToString();

            if (Entity.Tables[0].Rows[0]["KY"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.KY = Convert.ToInt16(Entity.Tables[0].Rows[0]["KY"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.KY = 0;
            }

            if (Entity.Tables[0].Rows[0]["LOAI_CHISO"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.LOAI_CHISO = Entity.Tables[0].Rows[0]["LOAI_CHISO"].ToString();
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.LOAI_CHISO = null;
            }

            if (Entity.Tables[0].Rows[0]["NGUOI_TAO"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.NGUOI_TAO = Entity.Tables[0].Rows[0]["NGUOI_TAO"].ToString();
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.NGUOI_TAO = null;
            }

            if (Entity.Tables[0].Rows[0]["NGAY_TAO"].ToString() != string.Empty)
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.NGAY_TAO = Convert.ToDateTime(Entity.Tables[0].Rows[0]["NGAY_TAO"]);
            }
            else
            {
                this.obj_NPC_TRTH_CHISO_Controller.pInfor.NGAY_TAO = null;
            }
        }

        #endregion
    }
}
